package com.ybear.ybcomponent.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ybear.ybcomponent.OnTouchListener;
import com.ybear.ybcomponent.Utils;
import com.ybear.ybcomponent.clicksub.ClickSubManage;

/**
 * 为{@link BaseRecyclerViewAdapter}使用的Holder
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {
    private final Context mContext;
    private final ClickSubManage mClickM;

    /*
    * touchStyleView 允许点击样式可以不是同一个View，
    * 因为Item可能会通过FrameLayout做层叠，点击效果是顶层，此时，底层的点击就不可见了。
    * */
    @NonNull
    private View mTouchStyleView;
    private final ViewGroup.LayoutParams mLayoutParams;

    private boolean isShow = true;

    private View.OnClickListener mClickListener;
    private View.OnLongClickListener mLongClickListener;

    public BaseViewHolder(@NonNull ViewGroup parent, @LayoutRes int itemRes) {
        this(LayoutInflater.from( parent.getContext() )
                .inflate(itemRes, parent, false)
        );
    }
    public BaseViewHolder(@NonNull ViewGroup parent, @LayoutRes int itemRes,
                          @LayoutRes int touchRes) {
        this(LayoutInflater.from( parent.getContext() )
                        .inflate(itemRes, parent, false),
                LayoutInflater.from( parent.getContext() )
                        .inflate(touchRes, parent, false)
        );
    }
    public BaseViewHolder(@NonNull View itemView) {
        this(itemView, itemView);
    }
    public BaseViewHolder(@NonNull View itemView, @NonNull View touchStyleView) {
        super(itemView);
        //订阅点击管理器
        mClickM = ClickSubManage.get();
        //用于按下的样式View默认是ItemView
        mTouchStyleView = touchStyleView;
        mContext = itemView.getContext();
        mLayoutParams = itemView.getLayoutParams();
        initClick();
    }

    private void initClick() {
        //点击事件
        mClickM.onClick( itemView );
        //长按事件
        mClickM.onLongClick( itemView );
    }
    
    public Context getContext() { return mContext; }

    /**
     * 显示View
     */
    public void showItemView() {
        View v = getItemView();
        v.setLayoutParams( mLayoutParams );
        v.setVisibility( View.VISIBLE );
        isShow = true;
    }

    /**
     * 隐藏View
     * @param isGone    是否完全隐藏
     */
    public void hideItemView(boolean isGone) {
        View v = getItemView();
        if( isGone ) {
            ViewGroup.LayoutParams lp = v.getLayoutParams();
            lp.width = lp.height = 0;
        }
        v.setVisibility( isGone ? View.GONE : View.INVISIBLE );
        isShow = false;
    }

    /**
     * 完全隐藏View
     */
    public void hideItemView() { hideItemView( true ); }

    /**
     * View是否处于显示状态
     * @return      状态
     */
    public boolean isShow() { return isShow; }

    /**
     * 获取View中的子View
     * @param id    子View id资源
     * @param <T>   子View泛型
     * @return      子View
     */
    public final <T extends View> T findViewById(@IdRes int id) {
        return itemView.findViewById( id );
    }

    @NonNull
    public View getItemView() { return itemView; }

    @NonNull
    public View getTouchStyleView() { return mTouchStyleView; }

    /**
     * 设置按下的样式View
     * @param v     默认：{@link #itemView}
     */
    public void setTouchStyleView(@NonNull View v) { mTouchStyleView = v; }

    /**
     * 优化版Touch
     * @param l     监听器
     */
    public void setOnSuperTouchListener(OnTouchListener l) {
        Utils.setOnSuperTouchListener( itemView, l );
    }

    /**
     * 原版Touch
     * @param l     监听器
     */
    public void setOnTouchListener(View.OnTouchListener l) {
        itemView.setOnTouchListener( l );
    }

    /**
     * 设置点击事件监听器
     * @param l     监听器
     */
    public boolean setOnClickListener(View.OnClickListener l) {
        if( mClickListener != null ) return false;
        mClickListener = l;
        addOnClickListener( l );
        return true;
    }

    /**
     * 移除设置点击事件监听器
     */
    public void removeSetOnClickListener() {
        mClickM.removeOnClickListener( itemView, mClickListener );
        mClickListener = null;
    }


    /**
     * 添加点击事件监听器
     * @param l     监听器
     */
    public void addOnClickListener(View.OnClickListener l) {
        mClickM.addOnClickListener( itemView, l );
    }

    /**
     * 移除点击事件监听器
     * @param l     监听器
     */
    public void removeOnClickListener(View.OnClickListener l) {
        mClickM.removeOnClickListener( itemView, l );
    }

    /**
     * 移除所有点击事件监听器
     */
    public void removeOnClickListenerAll() { mClickM.removeOnClickListener( itemView ); }

    /**
     * 设置长按事件监听器
     * @param l     监听器
     */
    public boolean setOnLongClickListener(View.OnLongClickListener l) {
        if( mLongClickListener != null ) return false;
        mLongClickListener = l;
        addOnLongClickListener( l );
        return true;
    }

    /**
     * 移除设置长按事件监听器
     */
    public void removeSetOnLongClickListener() {
        mClickM.removeOnLongClickListener( itemView, mLongClickListener );
        mLongClickListener = null;
    }
    
    /**
     * 添加长按事件监听器
     * @param l     监听器
     */
    public void addOnLongClickListener(View.OnLongClickListener l) {
        mClickM.addOnLongClickListener( itemView, l );
    }

    public void removeOnLongClickListener(View.OnLongClickListener l) {
        mClickM.removeOnLongClickListener( itemView, l );
    }
    /**
     * 移除所有长按事件监听器
     */
    public void removeOnLongClickListenerAll() { mClickM.removeOnLongClickListener( itemView ); }
}
